<template>
  <div class="userlist">
    <el-input v-model="input" placeholder="请输入内容" class="input-with-select" @change="search">
      <el-button slot="append" icon="el-icon-search" />
    </el-input>
    <el-button type="primary" @click="addUser">添加用户</el-button>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      :stripe="true"
      style="width: 100%"
    >
      <el-table-column
        label="#"
        type="index"
        width="60"
      />
      <el-table-column
        prop="username"
        label="姓名"
      />
      <el-table-column
        prop="email"
        label="邮箱"
      />
      <el-table-column
        prop="mobile"
        label="电话"
      />
      <el-table-column
        prop="role_name"
        label="角色"
      />
      <el-table-column
        label="状态"
        prop="mg_state"
      >
        <template slot-scope="{row}">
          <el-switch
            v-model="row.mg_state"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="OnEdit(row)" />
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="OnDelete(row)" />
          <el-button type="warning" size="mini" icon="el-icon-setting" @click="OnSet(row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <paging :total="total" @change-pagesize="ChangePageSize" @change-page="ChangePage" />
    <!-- 修改用户对话框 -->
    <el-dialog
      title="修改用户"
      :visible="dialogVisible"
      width="50%"
      @close="dialogVisible=false"
    >
      <template>
        <el-form :rules="rules" label-width="80px" :model="formLabelAlign">
          <el-form-item label="用户名">
            <el-input v-model.trim="formLabelAlign.username" :disabled="true" />
          </el-form-item>
          <el-form-item prop="email" label="邮箱:">
            <el-input v-model.trim="formLabelAlign.email" />
          </el-form-item>
          <el-form-item prop="mobile" label="手机号:">
            <el-input v-model.trim="formLabelAlign.mobile" />
          </el-form-item>
        </el-form>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加用户 -->
    <add-user :show="isShow" @change-show="isShow=$event" @OnSuccess="getUserList" />
    <!-- 分配角色 -->
    <assign-roles :form="RolesForm" :show-roles="showRoles" @ChangeShow="showRoles=false" />
  </div>
</template>

<script>
import { getUserList, ChangeUser, getUserInfoById, editUserInfoById, DeleteUserInfo } from '@/api/user.js'
import AddUser from './components/AddUser.vue'
import AssignRoles from './components/AssignRoles.vue'
import paging from '@/components/paging'
export default {
  components: { AddUser, AssignRoles, paging },
  data() {
    return {
      showRoles: false, // 分配角色对话框显示
      isShow: false, // 添加用户对话框显示
      total: 0, // 总记录数
      input: '', // 搜索框内容
      value: false,
      userForm: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 2// 每页数量
      },
      tableData: [], // 表单数据
      dialogVisible: false,
      formLabelAlign: {
        username: '',
        email: '',
        mobile: ''
      }, // 对话框所需数据
      // 自定义效验规则
      rules: {
        email: [
          { required: true, message: '邮件名不能为空', trigger: 'blur' },
          { pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/, message: '邮件格式不正确', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号格式不正确', trigger: 'blur' }
        ]
      },
      RolesForm: {}// 分配角色所需数据
    }
  },
  created() {
    this.getUserList()
  },

  methods: {
    // 获取用户信息列表
    async getUserList() {
      const res = await getUserList(this.userForm)
      console.log(res)
      this.total = res.data.total
      this.tableData = res.data.users
    },
    // 改变用户状态
    async onChange(val) {
      await ChangeUser(val.id, val.mg_state)
      this.$message({
        message: '修改成功',
        type: 'success'
      })
    },
    // 编辑
    async OnEdit(val) {
      this.dialogVisible = true
      const res = await getUserInfoById(val.id)
      this.formLabelAlign = res.data
    },
    // 保存编辑
    async onSave() {
      await editUserInfoById(this.formLabelAlign)
      this.getUserList()
      this.dialogVisible = false
      this.$message({
        message: '请求成功',
        type: 'success'
      })
    },
    // 每页显示数量
    ChangePageSize(e) {
      this.userForm.pagesize = e
      this.getUserList()
    },
    // 切换页数
    ChangePage(e) {
      this.userForm.pagenum = e
      this.getUserList()
    },
    // 搜索框
    async search() {
      this.userForm.query = this.input
      const res = await getUserList(this.userForm)
      this.tableData = res.data.users
    },
    // 添加用户
    addUser() {
      this.isShow = true
    },
    // 删除用户
    OnDelete(val) {
      console.log(val.id)
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await DeleteUserInfo(val.id)
        this.getUserList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分配角色
    async OnSet(val) {
      this.showRoles = true
      const res = await getUserInfoById(val.id)
      this.RolesForm = res.data
    }
  }
}
</script>

<style scoped lang='scss'>
.userlist{
  padding: 20px;
  background-color: #fff;
}
.el-input{
  width: 404px;
}
.el-input-group{
  padding-right: 20px;
}
.el-table{
  margin: 15px 0;
}
.el-form-item__content{
  .el-input{
    width: 100%;
  }
}
</style>
